brute force dp strings two pointers *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll             long long int 
#define ulli           unsigned long long int 
#define li             long int 
#define ff(i,a,b)      for(int i=a;i<=b;i++)
#define fb(i,b,a)      for(int i=b;i>=a;i--)
#define w(t)           while(--t >= 0)
#define l(s)           s.length()
#define ci(n)          cin>>n;
#define fast           ios_base::sync_with_stdio(false);
#define sa(a,n)        sort(a,a+n)
#define sv(v)          sort(v.begin(),v.end())
#define cy             cout<<"YES\n"
#define cn             cout<<"NO\n"
#define nl             cout<<"\n"
#define minus          cout<<"-1\n";
#define vi             vector<int>
#define pb             push_back
#define tc             int t; cin>>t;
#define pp             pair<int,int>
#define input(a,n)     for(int i=0;i<n;i++) cin>>a[i];
#define mod            1000000007
#define co(n)          cout<<n;
#define ret            return 0
#define mi             map<int,int>
#define output(a,n)    for(int i=0;i<n;i++) cout<<a[i]<<" ";   

#define siz 10555
const int maxs = 55;
int n,m;
char str[siz];
int Z[siz];
int ans[maxs][siz];
int solve(char s,int k)
{
    int cot = 0,ans=0;
    for(int i=0; i<n; i++)
    {
        if(str[i] ==s) cot++;
        Z[i+1] = i+1 - cot;
    }
    for(int i=1; i<=n; i++)
    {
        int key = Z[i] - k;
        int id = lower_bound(Z,Z+i+1,key) - Z;
        ans = max(ans,i - id);
    }
    return ans;
}
int main()
{
    int val;
    char s;
    memset(ans,-1,sizeof(ans));
    scanf("%d",&n);
    scanf("%s",str);
    scanf("%d",&m);
    for(int i=1; i<=m; i++)
    {
        scanf("%d %c",&val,&s);
        if(ans[s-'a'][val]==-1)
        {
            ans[s-'a'][val] = solve(s,val);
        }
        printf("%d\n",ans[s-'a'][val]);
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols